package com.ibiaoke.boss.controller;


import com.ibiaoke.common.base.BaseController;
import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.consts.WeChatConsts;
import com.ibiaoke.common.response.Res;
import com.ibiaoke.common.utils.IPUtil;
import com.ibiaoke.customer.entity.Customer;
import com.ibiaoke.customer.entity.DepositRecorder;
import com.ibiaoke.customer.entity.vo.DepositVO;
import com.ibiaoke.customer.service.DepositRecorderService;
import com.ibiaoke.customer.service.DepositService;
import com.ibiaoke.marketing.dao.CouponDao;
import com.ibiaoke.marketing.entity.Coupon;
import com.ibiaoke.order.dao.PtOrderDao;
import com.ibiaoke.order.entity.Order;
import com.ibiaoke.order.service.OrderService;
import com.ibiaoke.order.service.PtOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@Controller
public class DepositController extends BaseController{

    @Autowired
    private DepositService depositService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private DepositRecorderService depositRecorderService;
    @Autowired
    private PtOrderDao ptOrderDao;
    @Autowired
    private CouponDao couponDao;

    /**
     * 获取账户余额
     * @return
     */
    @RequestMapping("/getDeposit")
    @ResponseBody
    public Res getDeposit() {
        Customer customer = getLoginUser();
        DepositVO deposit = depositService.getDeposit(customer.getCustomerId());
        Coupon coupon = couponDao.getCouponById(2373);
        return Res.create().addResponse("deposit", deposit);
    }

    /**
     * 余额充值(微信，支付宝)
     * @param money
     * @param payType
     * @return
     */
    @RequestMapping("/depositCharge")
    @ResponseBody
    public Res depositCharge(BigDecimal money, Integer payType, HttpServletRequest request) {
        Customer customer = getLoginUser();
        String openId= (String) getSession().getAttribute("xcxOpenid");
        String ip = IPUtil.getIp(request);
        return depositService.depositCharge(customer.getCustomerId(),money, payType,openId,ip);
    }

    /**
     * 退款
     * 更改订单状态、更改可用预存款金额
     */
    @ResponseBody
    @RequestMapping("/RefundBywallet")
    public  Res  RefundBywallet(Integer orderId,BigDecimal money){
        Customer customer = getLoginUser();
        //根据订单号，更改订单状态
        Order order = orderService.updateOrderStatus(orderId);
        //根据用户Id，更改（增加）可用预存款金额
        depositService.updatePrice(customer.getCustomerId(),money);
        //增加当前用户流水记录 表bk_deposit_recorder
        depositRecorderService.insertByRefund(customer.getCustomerId(),3,order.getOrderCode(),1,money);
        //修改订单状态为6
        ptOrderDao.upPtOrderStatus(orderId,6);
        return Res.create();
    }

    @ResponseBody
    @RequestMapping("/listDepositRecorder")
    public Res listDepositRecorder(Page page) {
        Customer customer = getLoginUser();
        List<DepositRecorder> depositRecorderList = depositRecorderService.listDepositRecorderByCustomerId(customer.getCustomerId(),page);
        return Res.create().addResponse("depositRecorderList", depositRecorderList).addResponse("page",page);
    }
}
